package main

import (
	"embed"
	"fmt"
	"github.com/zserge/lorca"
	"go-gui/boot"
	"go-gui/route"
	"log"
	"net"
	"net/http"
	"os"
	"os/signal"
	"runtime"
)

//go:embed resources
var fs embed.FS
var width int = 480
var height int = 320

func main() {
	args := []string{
		"--remote-allow-origins=*",
	}
	if runtime.GOOS == "linux" {
		args = append(args, "--class=Lorca")
	}
	var err error
	boot.Ui, err = lorca.New("", "", width, height, args...)
	if err != nil {
		log.Fatal(err)
	}
	defer boot.Ui.Close()
	route.Init()
	ln, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		log.Fatal(err)
	}
	defer ln.Close()
	go http.Serve(ln, http.FileServer(http.FS(fs)))
	boot.Ui.Load(fmt.Sprintf("http://%s/resources/view", ln.Addr()))
	sigc := make(chan os.Signal)
	signal.Notify(sigc, os.Interrupt)
	select {
	case <-sigc:
	case <-boot.Ui.Done():
	}
	log.Println("exiting...")
}
